package niit.game;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.media.SoundPool;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements Runnable {
	private GameControls controls;
	private Bitmap cloud, map;
	public SoundPool bulletcry;
	public int cry, gunbigbang;
	public Main main1;
	public static Point PositionTouching = null;
	public Bitmap scores, iconblood, nolife, mTimerShell;
	public static int soundeat_items, soundrocket;
	public static int back_music;
	Paint paint;
	Thread mThread;
	MyPlane myPlane;
	private SurfaceHolder holder;
	private GameLoopThread gameLoopThread;
	SpriteThread spriteThread;
	Vector<Enemy> enemy = null;
	static List<Temp> temps = new ArrayList<Temp>();
	Map mMap;
	// Vector<Bullet> bulletsEne = new Vector<Bullet>();
	boolean isHold = true;
	int x1;

	public void NewGame() {
		MyPlane.newGame();
	}

	public void resume() {
		gameLoopThread.running = true;
		mThread = new Thread(this);
		mThread.start();
	}

	public void pause() {
		gameLoopThread.running = false;
		while (true) {
			try {
				mThread.join();
				break;
			} catch (InterruptedException e) {
			}
		}
	}

	public GameView(Main main1) {
		super(main1);
		NewGame();

		gameLoopThread = new GameLoopThread(this);

		holder = getHolder();
		holder.addCallback(new Callback() {

			public void surfaceDestroyed(SurfaceHolder holder) {

				boolean retry = true;
				gameLoopThread.setRunning(false);
				while (retry) {
					try {
						gameLoopThread.join();
						retry = false;
					} catch (InterruptedException e) {
					}
				}

			}

			public void surfaceCreated(SurfaceHolder holder) {

				gameLoopThread.setRunning(true);
				gameLoopThread.start();

			}

			public void surfaceChanged(SurfaceHolder holder, int format,
					int width, int height) {

			}
		});
		mTimerShell = BitmapFactory.decodeResource(getResources(),
				R.drawable.int_timer);
		nolife = BitmapFactory
				.decodeResource(getResources(), R.drawable.nolife);
		iconblood = BitmapFactory.decodeResource(getResources(),
				R.drawable.blood);
		scores = BitmapFactory
				.decodeResource(getResources(), R.drawable.scores);

		cloud = BitmapFactory.decodeResource(getResources(), R.drawable.may2);
		map = BitmapFactory.decodeResource(getResources(), R.drawable.map);
		paint = new Paint();
		paint.setColor(Color.RED);
		mMap = new Map(this, map, cloud);
		controls = new GameControls(this,
				(Bitmap) BitmapFactory.decodeResource(getResources(),
						R.drawable.plane2));

		myPlane = new MyPlane(this, controls,
				(Bitmap) BitmapFactory.decodeResource(getResources(),
						R.drawable.plane2));
		setOnTouchListener(controls);
		enemy = new Vector<Enemy>();

	}

	public void addEnemy() {
		enemy.add(new Enemy(this, this.getWidth(), 1, (Bitmap) BitmapFactory
				.decodeResource(getResources(), R.drawable.enemy1)));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		mMap.onDraw(canvas);
		canvas.drawBitmap(mTimerShell,
				Main.SCREENWIDTH - mTimerShell.getWidth(), 0, null);
		canvas.drawBitmap(iconblood,
				Main.SCREENWIDTH - 13 * mTimerShell.getWidth() / 20, 1, null);
		canvas.drawText(MyPlane.health + "", Main.SCREENWIDTH - 13
				* mTimerShell.getWidth() / 20 + 5 + iconblood.getWidth(), 15,
				paint);
		canvas.drawBitmap(
				nolife,
				Main.SCREENWIDTH - 13 * mTimerShell.getWidth() / 20
						+ iconblood.getWidth() + 25, 1, null);
		canvas.drawText(MyPlane.life + "",
				Main.SCREENWIDTH - 13 * mTimerShell.getWidth() / 20 + 30
						+ nolife.getWidth() + iconblood.getWidth(), 15, paint);
		canvas.drawBitmap(scores,
				Main.SCREENWIDTH - 13 * mTimerShell.getWidth() / 20 + 40
						+ nolife.getWidth() + iconblood.getWidth(), 1, null);
		canvas.drawText(MyPlane.CurrentScore + "", Main.SCREENWIDTH - 13
				* mTimerShell.getWidth() / 20 + 45 + nolife.getWidth()
				+ iconblood.getWidth() + scores.getWidth(), 15, paint);

		controls.update(null);
		
		canvas.drawBitmap(MyPlane.plan, MyPlane.PlanePoint.x,
				MyPlane.PlanePoint.y, null);

		Log.d("Chay",
				"Goi onDraw của gameview: size dan:" + myPlane.bullets.size());
		for (int i = 0; i < myPlane.bullets.size(); i++) {
			switch (myPlane.bullets.get(i).getDirect()) {
			case 1:
				myPlane.bullets.get(i).onDraw1(canvas);
				break;
			// case 2:
			// myPlane.bullets.get(i).onDraw2(canvas);
			// break;
			// case 3:
			// myPlane.bullets.get(i).onDraw3(canvas);
			// break;

			default:
				break;
			}

			for (int j = 0; j < myPlane.bullets.size(); j++) {
				if (myPlane.bullets.get(j).y < 0) {
					myPlane.bullets.remove(j);
				}
			}

		}

		for (int i = 0; i < enemy.size(); i++) {
			enemy.get(i).onDraw(canvas);
		}
		MyPlane.EneVsPlan(this);

		// for (int i = 0; i < Enemy.bullet.size(); i++) {
		//
		// // switch (Enemy.bullet.get(i).getDirect()) {
		// // case 1:
		// Enemy.bullet.get(i).onDraw(canvas);
		// break;
		// // case 2:
		// // myPlane.bullets.get(i).onDraw2(canvas);
		// // break;
		// // case 3:
		// // myPlane.bullets.get(i).onDraw3(canvas);
		// // break;
		//
		// // default:
		// // break;
		// // }
		// }
		for (int i = temps.size() - 1; i >= 0; i--) {
			temps.get(i).onDraw(canvas);
		}

	}

	public void run() {

	}

}
